const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
exports.main = async (event, context) => {
  console.log(event)
  const target_db = db.collection('test')
  try {
    return await target_db.aggregate().lookup({
        from: 'challenge',
        localField: '_id',
        foreignField: 'test_id',
        as: 'challenge',
      }).match({
        "challenge.user_openid": event.openid
      })
      .sort({
        update_time: -1,
      })
      .skip(20 * event.skip)
      .limit(event.limit)
      .end()
  } catch (e) {
    console.log(e)
  }
}